from config.MyEnv import MyEnv
from config.db import MySQLClient
import requests
from flask import jsonify
import curlify
import re
from config.db_config import db_config, tenant_db_config, refact_db_config

db_client = MySQLClient(db_config)
tenant_db_client = MySQLClient(tenant_db_config)
refact_db_client = MySQLClient(refact_db_config)


def get_records(action=None,type=None):
    sql = ""
    params = []
    sql += ("SELECT id, biz_id, app_code, pc_code, channels, action,JSON_EXTRACT(request_body, '$.businessGuid') as "
            "businessGuid FROM ") + \
           MyEnv.table_name[action]
    sql += " WHERE biz_id>''"
    if action == "message_send":
        sql += "AND action='message_send' AND request_type=1 AND request_mode=11 AND request_method='channelSend'"
    if action == "scene_send":
        sql += " AND action='message_send' AND request_type=1 AND request_mode=12 AND request_method='sceneSend'"
        if type == "1":
            sql += " AND scene_code in ('check-check-emergency-need-notify-user-cc','check-measure-all-to-inspected-user-cc','check-measure-all-to-repair-user-cc','check-measure-all-to-repaired-user-cc','material-approval-auditor','material-assign-user','material-reject-auditor','material-three-apply-entry-for-inspect-user-cc','material-three-apply-entry-for-review-inspect-user-cc','material-three-apply-entry-for-review-inspect-user-todo','material-three-area-inspect-for-check-user-cc','material-three-area-inspect-for-check-user-todo','material-three-area-inspect-for-for-exit-cc','material-three-area-inspect-for-inspected-user-cc','material-three-check-for-upload-report-user-cc','material-three-check-for-upload-report-user-todo','material-three-for-exit-for-exited-user-cc','material-three-inspect-for-area-inspect-user-cc','material-three-inspect-for-area-inspect-user-todo','material-three-inspect-for-check-user-cc','material-three-inspect-for-for-exit-user-cc','material-three-inspect-for-inspect-user-cc','material-three-inspect-for-inspected-user-cc','material-three-recall-for-apply-entry-user-cc','material-three-recheck-for-upload-report-user-cc','material-three-review-inspect-for-inspect-user-cc','material-three-review-inspect-for-inspect-user-todo','material-three-revoke-for-for-apply-entry-user-revoke-inspector','material-three-save-task-for-apply-entry-user-cc','material-three-save-task-for-apply-entry-user-todo','material-three-save-task-for-for-apply-entry-user-save-task-inspector','material-three-send-back-for-apply-entry-user-cc','material-three-upload-report-for-check-passed-user-cc','material-three-upload-report-for-recheck-passed-user-cc','material-three-upload-report-for-recheck-user-cc','material-three-upload-report-for-recheck-user-todo','material-three-walkoff-for-exited-user-todo','material-three-walkoff-for-for-exit-user-todo','material-withdrawal-auditor','measure-approval-auditor','measure-assign-user','measure-reject-auditor','measure-withdrawal-auditor','problem-check-approval-auditor','problem-check-assign-user','problem-check-closed-user-cc','problem-check-closed-user-register','problem-check-examine-repair-approved-user-cc','problem-check-examine-repair-approved-user-repair','problem-check-regist-examine-user-cc','problem-check-regist-examine-user-examine','problem-check-regist-repair-user-cc','problem-check-regist-repair-user-repair','problem-check-reinspect-passed-approved-user-cc','problem-check-reinspect-passed-approved-user-register','problem-check-reinspect-repair-recall-user-cc','problem-check-reinspect-repair-recall-user-reinspect','problem-check-reinspect-repair-reject-user-cc','problem-check-reinspect-repair-reject-user-register','problem-check-reinspect-repair-reject-user-repair','problem-check-reinspect-review-approved-user-cc','problem-check-reinspect-review-approved-user-register','problem-check-reinspect-review-approved-user-review','problem-check-reject-auditor','problem-check-repair-draft-recall-user-cc','problem-check-repair-draft-recall-user-repair','problem-check-repair-reinspect-approved-user-cc','problem-check-repair-reinspect-approved-user-register','problem-check-repair-reinspect-approved-user-reinspect','problem-check-review-passed-approved-user-cc','problem-check-review-passed-approved-user-register','problem-check-review-reinspect-reject-user-cc','problem-check-review-reinspect-reject-user-register','problem-check-review-reinspect-reject-user-reinspect','problem-check-withdrawal-auditor','process-accept-approval-auditor','process-accept-assign-user','process-accept-problem-approval-auditor','process-accept-problem-assign-user','process-accept-problem-reject-auditor','process-accept-problem-withdrawal-auditor','process-accept-reject-auditor','process-accept-withdrawal-auditor','process-accept-workflow-accept-user-cc','process-accept-workflow-accept-user-todo','process-accept-workflow-accepted-user-cc','process-accept-workflow-accepted-user-done','process-accept-workflow-rejected-user-cc','process-accept-workflow-rejected-user-todo','process-accept-workflow-report-user-todo','process-accept-workflow-spot-accepted-user-cc','process-accept-workflow-spot-accepted-user-done','process-urge-problem-check-user-todo','special-check-approval-auditor','special-check-assign-user','special-check-reject-auditor','special-check-withdrawal-auditor','special-complete-task-relatedmembers','special-create-task-inchargeby','special-create-task-managements','special-create-task-members','special-inspect-close-checkquality-problem-cc','special-inspect-close-checkquality-problem-regist','special-inspect-close-module-problem-cc','special-inspect-close-module-problem-regist','special-inspect-publish-task-cc','special-inspect-publish-task-charge','special-inspect-publish-task-management','special-inspect-publish-task-user','special-inspect-quickreport-publish-task-cc','special-inspect-regist-checkquality-problem-cc','special-inspect-regist-checkquality-problem-repair','special-inspect-regist-module-problem','special-inspect-reinspect-checkquality-problem-pass-cc','special-inspect-reinspect-checkquality-problem-pass-regist','special-inspect-reinspect-checkquality-problem-recall-cc','special-inspect-reinspect-checkquality-problem-recall-regist','special-inspect-reinspect-checkquality-problem-recall-repair','special-inspect-reinspect-module-problem-pass-cc','special-inspect-reinspect-module-problem-pass-regist','special-inspect-reinspect-module-problem-send-back-cc','special-inspect-reinspect-module-problem-send-back-regist','special-inspect-reinspect-module-problem-send-back-repair','special-inspect-repair-checkquality-problem-pass-cc','special-inspect-repair-checkquality-problem-pass-regist','special-inspect-repair-checkquality-problem-pass-reinspect','special-inspect-repair-module-problem-pass-cc','special-inspect-repair-module-problem-pass-regist','special-inspect-repair-module-problem-pass-reinspect','special-inspect-repair-module-problem-recall-cc','special-inspect-repair-module-problem-recall-repair','special-inspect-report-publish-task-cc','special-inspect-sampling-module-problem-send-back-reinspect-cc','special-inspect-sampling-module-problem-send-back-reinspect-regist','special-inspect-sampling-module-problem-send-back-reinspect-reinspect','special-inspect-sampling-module-problem-send-back-repair-cc','special-inspect-sampling-module-problem-send-back-repair-regist','special-inspect-sampling-module-problem-send-back-repair-repair','templet-detail-approval-auditor','templet-detail-assign-user','templet-detail-reject-auditor','templet-detail-withdrawal-auditor','templet-problem-approval-auditor','templet-problem-assign-user','templet-problem-reject-auditor','templet-problem-withdrawal-auditor')"
        if type == "2":
            sql += " AND scene_code in ('material-approval-cc','material-approval-submit','material-archive-auditor','material-archive-cc','material-archive-submit','material-invalid-auditor','material-invalid-cc','material-invalid-submit','material-perusal-user','material-reject-cc','material-reject-submit','material-urge-auditor','material-withdrawal-cc','measure-approval-cc','measure-approval-submit','measure-archive-auditor','measure-archive-cc','measure-archive-submit','measure-invalid-auditor','measure-invalid-cc','measure-invalid-submit','measure-negotiate-user','measure-perusal-user','measure-reject-cc','measure-reject-submit','measure-submit-check-data-cc','measure-termination-auditor','measure-termination-cc','measure-termination-submit','measure-timeout-auditor','measure-urge-auditor','measure-withdrawal-cc','problem-check-approval-cc','problem-check-approval-submit','problem-check-archive-auditor','problem-check-archive-cc','problem-check-archive-submit','problem-check-invalid-auditor','problem-check-invalid-cc','problem-check-invalid-submit','problem-check-negotiate-user','problem-check-perusal-user','problem-check-reject-cc','problem-check-reject-submit','problem-check-termination-auditor','problem-check-termination-cc','problem-check-termination-submit','problem-check-timeout-auditor','problem-check-urge-auditor','problem-check-withdrawal-cc','process-accept-approval-cc','process-accept-approval-submit','process-accept-archive-auditor','process-accept-archive-cc','process-accept-archive-submit','process-accept-invalid-auditor','process-accept-invalid-cc','process-accept-invalid-submit','process-accept-perusal-user','process-accept-problem-approval-cc','process-accept-problem-approval-submit','process-accept-problem-archive-auditor','process-accept-problem-archive-cc','process-accept-problem-archive-submit','process-accept-problem-invalid-auditor','process-accept-problem-invalid-cc','process-accept-problem-invalid-submit','process-accept-problem-negotiate-user','process-accept-problem-perusal-user','process-accept-problem-reject-cc','process-accept-problem-reject-submit','process-accept-problem-termination-auditor','process-accept-problem-termination-cc','process-accept-problem-termination-submit','process-accept-problem-timeout-auditor','process-accept-problem-urge-auditor','process-accept-problem-withdrawal-cc','process-accept-reject-cc','process-accept-reject-submit','process-accept-timeout-auditor','process-accept-urge-auditor','process-accept-withdrawal-cc','special-check-approval-cc','special-check-approval-submit','special-check-archive-auditor','special-check-archive-cc','special-check-archive-submit','special-check-invalid-auditor','special-check-invalid-cc','special-check-invalid-submit','special-check-negotiate-user','special-check-perusal-user','special-check-reject-cc','special-check-reject-submit','special-check-termination-auditor','special-check-termination-cc','special-check-termination-submit','special-check-timeout-auditor','special-check-urge-auditor','special-check-withdrawal-cc','special-complete-task-cc','special-create-task-cc','templet-detail-approval-cc','templet-detail-approval-submit','templet-detail-archive-auditor','templet-detail-archive-cc','templet-detail-archive-submit','templet-detail-invalid-auditor','templet-detail-invalid-cc','templet-detail-invalid-submit','templet-detail-perusal-user','templet-detail-reject-cc','templet-detail-reject-submit','templet-detail-timeout-auditor','templet-detail-urge-auditor','templet-detail-withdrawal-cc','templet-problem-approval-cc','templet-problem-approval-submit','templet-problem-archive-auditor','templet-problem-archive-cc','templet-problem-archive-submit','templet-problem-invalid-auditor','templet-problem-invalid-cc','templet-problem-invalid-submit','templet-problem-negotiate-user','templet-problem-perusal-user','templet-problem-reject-cc','templet-problem-reject-submit','templet-problem-termination-auditor','templet-problem-termination-cc','templet-problem-termination-submit','templet-problem-timeout-auditor','templet-problem-urge-auditor','templet-problem-withdrawal-cc')"
    if action == "pending_send":
        sql += "AND action='pending_send'"
    sql += " ORDER BY RAND() LIMIT 20"
    print(sql)
    return db_client.query(sql, params)


def do_api(url, auth, tenant_code, request_body):
    headers = {
        "my-api-Authorization": auth,
        "tenantCode": tenant_code,
        "Content-Type": "application/json"
    }
    response = requests.post(url, headers=headers, data=request_body.encode("utf-8"))
    curl_cmd = curlify.to_curl(response.request, compressed=True)
    # 用正则去除 -H 'Content-Length: xxx'
    curl_cmd = re.sub(r"-H 'Content-Length: \d+'\s*", '', curl_cmd)
    return jsonify({
        'curl': curl_cmd,
        'response': response.text
    })

def get_request_body(action, request_id):
    sql = "SELECT request_body FROM " + MyEnv.table_name[action] + " WHERE id=%s"
    result = db_client.query(sql, [request_id])
    return result[0][0] if result else None
